#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int countBit(vector<int>& bit)
    {
        int res = 0;
        for (int i = 0; i < 32; i++)
        {
            if (bit[i] > 0)
                res |= 1 << i;
        }
        return res;
    }
    void addBit(vector<int>& bit, int input)
    {
        int res = 0;
        for (int i = 0; i < 32; i++)
        {
            if (input & (1 << i))
                bit[i]++;
        }
    }
    void minBit(vector<int>& bit, int input)
    {
        int res = 0;
        for (int i = 0; i < 32; i++)
        {
            if (input & (1 << i))
                bit[i]--;
        }
    }

    int minimumSubarrayLength(vector<int>& nums, int k) {
        if (k == 0) return 1;
        vector<int> bit(32, 0);
        int left = 0, right = 0, n = nums.size();
        int res = INT_MAX;
        while (right < n)
        {
            while (right < n && countBit(bit) < k)
            {
                addBit(bit, nums[right++]);
            }
            res = min(res, right - left);
            if (res == n && countBit(bit) < k) return -1;
            while (left < right && countBit(bit) >= k)
            {
                res = min(res, right - left);
                minBit(bit, nums[left++]);
            }
        }
        return res;
    }
};